package com.pug.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pug.bo.UserOrderBo;
import com.pug.dto.UserOrderDto;
import com.pug.mapper.order.UserOrderMapper;
import com.pug.pojo.KssUser;
import com.pug.pojo.UserOrder;
import com.pug.service.BaseService;
import com.pug.threadlocal.UserThreadLocal;
import com.pug.utils.fn.asserts.Vsserts;
import com.pug.vo.UserOrderVo;
import com.pug.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/4/2$ 21:01$
 */
@Service
@Slf4j
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder> implements IUserOrderService, BaseService {


    @Autowired
    private UserOrderProducer userOrderProducer;

    /**
     * 用户下单处理
     *
     * @param userVo
     * @return
     */
    @Override
    @Transactional
    public UserOrderBo createUserOrder(UserVo userVo) {
        // 1: 获取当前下单的用户信息
//        KssUser kssUser = UserThrealLocal.get();
        // 2: 创建用户订单实例
        KssUser kssUser=new KssUser();
        UserOrder userOrder = new UserOrder();
        userOrder.setOrderno(userVo.getOrderNo());
        userOrder.setStatus(0);// 代表未支付

        userOrder.setUserId(kssUser.getId());
        userOrder.setNickname(kssUser.getNickname());
        userOrder.setAvatar(kssUser.getAvatar());
        log.info("用户开始下单，订单信息是： 【{}】", userOrder);

        // 3：执行数据的订单保存功能
        boolean b = this.saveOrUpdate(userOrder);
        if (b) {
            // 4:获取到订单信息开始进行异步发送处理
            UserOrderBo userOrderBo = tranferBo(userOrder, UserOrderBo.class);
            // 5: 对内进行发送异步处理
            UserOrderDto userOrderDto = tranferBo(userOrder, UserOrderDto.class);
            // 6 : 使用mq进行异步处理
            userOrderProducer.pushOrder(userOrderDto);
            return userOrderBo;
        } else {
            return null;
        }
    }
    @Override
    public Page<UserOrder> findUserOrders(UserOrderVo userOrderVo) {
        KssUser user = UserThreadLocal.get();
        // 1: 设置查询的条件
        Page<UserOrder> page = new Page<>(userOrderVo.getPageNo(), userOrderVo.getPageSize());
        // 2: 设置条件
        LambdaQueryWrapper<UserOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Vsserts.isNotNull(userOrderVo.getPaystatus()),
                UserOrder::getStatus, userOrderVo.getPaystatus());
        // 3: 查询某个用户的订单信息
        lambdaQueryWrapper.eq(UserOrder::getUserId, user.getId());
        // 4:可以根据订单编号查询或者标题查询
//        lambdaQueryWrapper.like(Vsserts.isNotNull(userOrderVo.getKeyword()),
//                UserOrder::getHotelTitle, userOrderVo.getKeyword());
        // 5: 考虑排序
        lambdaQueryWrapper.orderByDesc(UserOrder::getCreateTime);
        return this.page(page, lambdaQueryWrapper);
    }
    /**
     * 查询用户订单状态信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectOrderStatusInfo() {
        KssUser user = UserThreadLocal.get();
        return this.baseMapper.selectOrderStatusInfo(user.getId());
    }

}
